<?php

namespace App\Http\Controllers\Mp;

use App\Http\Controllers\Controller;
use App\Http\Service\DeviceService;
use App\Model\AreasPrice;
use App\Model\Business;
use App\Model\BusinessConf;
use App\Model\Device;
use App\Utils\Result;
use Illuminate\Http\Request;
use function Symfony\Component\String\s;

class BusinessConfController extends Controller
{
    public function index(Request $request)
    {
        $businessId = $request->input('businessId', $this->getBusinessId());
        if (!$businessId) {
            return Result::error(401, '参数错误');
        }
        $data = BusinessConf::query()->where('business_id', $businessId)->first();
        if (!$data) {
            $data = new BusinessConf();
            $data->business_id = $businessId;
            $data->save();
        }

        return Result::success($data);
    }

    public function update(Request $request)
    {
        $res = BusinessConf::query()->where('business_id', $this->getBusinessId())->first();
        $res->fill($request->all());
        $res->save();
        return Result::success($res);
    }

    /**修改峰谷表价格
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateCoef(Request $request){
        try{
            $sn= $request->input('sn');
            $data = $request->only(['coef1','coef2','coef3','coef4']);
            $device = Device::where('sn',$sn)->first();
            $device->fill($data);
            $device->save();
            return Result::success();
        }catch (\Exception $e){
            return Result::error();
        }
    }


    public function getLvPrice(Request $request){
        try{
            $adcode = $request->input('adcode');
            $deviceId = $request->input('device_id');
            $list = AreasPrice::when(!empty($adcode), function ($query) use ($adcode) {
                $query->where('adcode', $adcode );
                $query->where('device_id',0);
            })
            ->when(!empty($deviceId), function ($query) use ($deviceId) {
                    $query->where('device_id', $deviceId );
                })
                    ->with('area')
                    ->orderBy('season')
                    ->orderBy('month')
                    ->orderBy('start')
                    ->get();
            return Result::success($list);
        }catch (\Exception $e){
            return Result::error(1001,$e->getMessage().$e->getTraceAsString());
        }
    }

    public function makeFg(Request $request){
        $sn = $request->input('sn');
        $device = Device::query()->where('sn',$sn)->first();
        $data = $device->makeFg();
        if($data){
            $service = app()->make(DeviceService::class);
            $result = $service->sendFgb($device->mac,$data);
            return Result::success($result);
        }
        return Result::error(404,'请先设置峰谷参数');
    }


    public function createLvPrice(Request $request){
        try{
            $adcode =$request->input('adcode');
            $season = $request->input('season',1);
            $deviceId = $request->input('device_id');
            if(!$adcode){
                return Result::error(404,'请先选择地区');
            }
            $months = $request->input('month');
            $months = json_decode($months,true);
            $data = $request->input('data');
            $data = json_decode($data,true);

            foreach($months as $month){
                AreasPrice::query()->where('device_id',$deviceId)
                    ->where('month',$month)->delete();
                foreach ($data as $item) {
                    $model = new AreasPrice();
                    $model->start = $item['start'];
                    $model->end = $item['end'];
                    $model->coef_type = $item['coef_type'];
                    $model->month = $month;
                    $model->adcode = $adcode;
                    $model->season = $season;
                    $model->device_id = $deviceId;
                    $model->save();
                }
            }
            return Result::success();
        }catch (\Exception $e){
           return Result::error(1001,$e->getMessage().$e->getTraceAsString());
        }
    }
}
